package cn.edu.zut.sunday.blimp.func;

import cn.edu.zut.sunday.blimp.domain.DatasetName;
import cn.edu.zut.sunday.blimp.domain.ExampleInfo;
import cn.edu.zut.sunday.blimp.vo.ExampleInfoVo;
import cn.edu.zut.sunday.utils.DateUtils;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;

@UtilityClass
public class ExampleInfoTransformUtils {

    /**
     * 转换ExampleInfo实体对象成ExampleInfoVo视图对象
     */
    public static final Function<ExampleInfo, ExampleInfoVo> ENTITY_2_VO = exampleInfo -> {
        final ExampleInfoVo exampleInfoVo = new ExampleInfoVo();
        exampleInfoVo.setId(exampleInfo.getId());
        exampleInfoVo.setDatasetInfo(getDatasetInfo(exampleInfo.getDatasetName()));
        exampleInfoVo.setSentenceGood(exampleInfo.getSentenceGood());
        exampleInfoVo.setSentenceBad(exampleInfo.getSentenceBad());
        exampleInfoVo.setField(exampleInfo.getField());
        exampleInfoVo.setLinguisticsTerm(exampleInfo.getLinguisticsTerm());
        exampleInfoVo.setUid(exampleInfo.getUID());
        exampleInfoVo.setCreateTime(DateUtils.format(exampleInfo.getDateCreated()));
        exampleInfoVo.setUpdateTime(DateUtils.format(exampleInfo.getDateModified()));

        return exampleInfoVo;
    };

    /**
     * 转换ExampleInfo实体对象列表成ExampleInfoVo视图对象列表
     */
    public static final Function<List<ExampleInfo>, List<ExampleInfoVo>> LIST_ENTITY_2_VO = list -> list.stream()
      .map(ExampleInfoTransformUtils.ENTITY_2_VO)
      .collect(Collectors.toList());

    /**
     * 产生"数据库名/id"的字符串形式
     *
     * @param datasetName 所属数据库对象
     * @return "数据库名/id"
     */
    private static String getDatasetInfo(DatasetName datasetName) {
        String        format  = "%s/%s";
        final Integer id      = datasetName.getId();
        final String  setName = datasetName.getSetName();
        return String.format(format, setName, id);
    }
}
